﻿/*
Created By VietRise on 4/6/2014
*/

#include "ScreenGameplay.h"
#include "ScreenLevel.h"

ScreenGameplay* ScreenGameplay::_instance = NULL;

ScreenGameplay::~ScreenGameplay()
{
	CC_SAFE_RELEASE(_buttons);
	CC_SAFE_RELEASE(_levelPool);
	CC_SAFE_RELEASE(_bombPool);
	CC_SAFE_RELEASE(_diamondPool);
	CC_SAFE_RELEASE(_holePool);
	CC_SAFE_RELEASE(_mousePool);
	CC_SAFE_RELEASE(_oldManPool);
}

ScreenGameplay::ScreenGameplay()
{
	_screenSize = CCDirector::sharedDirector()->getWinSize();
	_state = STATEGAMEPLAY::sStart;

	std::string levelsFile = CCFileUtils::sharedFileUtils()->fullPathForFilename("levels.plist");
	_levelPool = CCArray::createWithContentsOfFileThreadSafe(levelsFile.c_str());
	_levelPool->retain();

	createScreen();
	createPools();
	createActions();

	setTouchEnabled(true);
}

CCScene* ScreenGameplay::scene(int level, int levelCompleted)
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	ScreenGameplay *layer = ScreenGameplay::create(level, levelCompleted);

	// add layer as a child to scene
	scene->addChild(layer);

	return scene;
}

ScreenGameplay* ScreenGameplay::create(int level, int levelCompleted)
{
	if (!_instance)
	{
		_instance = new ScreenGameplay();
	}
	else
	{
		_instance->clearLayer(true);
	}
	_instance->setLevelCompleted(levelCompleted);
	_instance->loadLevel(level);
	_instance->scheduleUpdate();

	return _instance;
}

void ScreenGameplay::update(float dt)
{
	if (_state == STATEGAMEPLAY::sInGame)
	{
		if (_noHitBomb)
		{
			// Update for bomb
			Bomb* bomb;
			for (int i = 0; i < _numberBombOnScreen; i++)
			{
				bomb = (Bomb*)_bombPool->objectAtIndex(i);
				if (!bomb->isVisible() && bomb->getTimeDelay() == 0)
				{
					bomb->initTimeDelay(_durationDiamondDelay[0], _durationDiamondDelay[1]);
					bomb->initTimeAppear(_durationDiamondAppearance[0], _durationDiamondAppearance[1]);
				}
				else if (!bomb->isVisible() && bomb->getTimeDelay() != 0)
				{
					bomb->setCountTimeDelay(bomb->getCountTimeDelay() + dt);
					if (bomb->getCountTimeDelay() >= bomb->getTimeDelay())
					{
						bomb->setCountTimeDelay(0);
						bomb->setTimeDelay(0);

						int hole;
						while (true)
						{
							hole = rand() % _holePool->count();
							if (((Hole*)_holePool->objectAtIndex(hole))->getState() == STATEHOLE::sBlank)
							{
								((Hole*)_holePool->objectAtIndex(hole))->setState(STATEHOLE::sBusy);
								bomb->setPosition(ccp(((Hole*)_holePool->objectAtIndex(hole))->getPosition().x, ((Hole*)_holePool->objectAtIndex(hole))->getPosition().y + 20));
								bomb->appear(hole);
								break;
							}
						}

					}
				}
			}

			// Update for diamond
			Diamond* diamond;
			for (int i = 0; i < _numberDiamondOnScreen; i++)
			{
				diamond = (Diamond*)_diamondPool->objectAtIndex(i);
				if (!diamond->isVisible() && diamond->getTimeDelay() == 0)
				{
					diamond->initTimeDelay(_durationDiamondDelay[0], _durationDiamondDelay[1]);
					diamond->initTimeAppear(_durationDiamondAppearance[0], _durationDiamondAppearance[1]);
				}
				else if (!diamond->isVisible() && diamond->getTimeDelay() != 0)
				{
					diamond->setCountTimeDelay(diamond->getCountTimeDelay() + dt);
					if (diamond->getCountTimeDelay() >= diamond->getTimeDelay())
					{
						diamond->setCountTimeDelay(0);
						diamond->setTimeDelay(0);

						int hole;
						while (true)
						{
							hole = rand() % _holePool->count();
							if (((Hole*)_holePool->objectAtIndex(hole))->getState() == STATEHOLE::sBlank)
							{
								((Hole*)_holePool->objectAtIndex(hole))->setState(STATEHOLE::sBusy);
								diamond->setPosition(ccp(((Hole*)_holePool->objectAtIndex(hole))->getPosition().x, ((Hole*)_holePool->objectAtIndex(hole))->getPosition().y + 25));
								diamond->appear(hole);
								break;
							}
						}

					}
				}
			}

			// Update for mouse
			Mouse* mouse;
			for (int i = 0; i < _numberMouseOnScreen; i++)
			{
				mouse = (Mouse*)_mousePool->objectAtIndex(i);
				if (!mouse->isVisible() && mouse->getTimeDelay() == 0)
				{
					mouse->initTimeDelay(_durationMouseDelay[0], _durationMouseDelay[1]);
					mouse->initTimeAppear(_durationMouseAppearance[0], _durationMouseAppearance[1]);
				}
				else if (!mouse->isVisible() && mouse->getTimeDelay() != 0)
				{
					mouse->setCountTimeDelay(mouse->getCountTimeDelay() + dt);
					if (mouse->getCountTimeDelay() >= mouse->getTimeDelay())
					{
						mouse->setCountTimeDelay(0);
						mouse->setTimeDelay(0);

						int hole;
						while (true)
						{
							hole = rand() % _holePool->count();
							if (((Hole*)_holePool->objectAtIndex(hole))->getState() == STATEHOLE::sBlank)
							{
								((Hole*)_holePool->objectAtIndex(hole))->setState(STATEHOLE::sBusy);
								mouse->setPosition(ccp(((Hole*)_holePool->objectAtIndex(hole))->getPosition().x, ((Hole*)_holePool->objectAtIndex(hole))->getPosition().y + 25));
								mouse->appear(hole);
								break;
							}
						}

					}
				}


			}

			// Update for OldMan
			OldMan* oldMan;
			if (_countOldManAppear < _numberOldManAppearance)
			{
				for (int i = 0; i < _numberOldManOnScreen; i++)
				{
					oldMan = (OldMan*)_oldManPool->objectAtIndex(i);
					if (!oldMan->isVisible() && oldMan->getTimeDelay() == 0)
					{
						oldMan->initTimeDelay(_durationOldManDelay[0], _durationOldManDelay[1]);
						oldMan->initTimeAppear(_durationOldManAppearance[0], _durationOldManAppearance[1]);
					}
					else if (!oldMan->isVisible() && oldMan->getTimeDelay() != 0)
					{
						oldMan->setCountTimeDelay(oldMan->getCountTimeDelay() + dt);
						if (oldMan->getCountTimeDelay() >= oldMan->getTimeDelay())
						{
							oldMan->setCountTimeDelay(0);
							oldMan->setTimeDelay(0);

							int hole;
							while (true)
							{
								hole = rand() % _holePool->count();
								if (((Hole*)_holePool->objectAtIndex(hole))->getState() == STATEHOLE::sBlank)
								{
									((Hole*)_holePool->objectAtIndex(hole))->setState(STATEHOLE::sBusy);
									break;
								}
							}
							oldMan->setPosition(ccp(((Hole*)_holePool->objectAtIndex(hole))->getPosition().x, ((Hole*)_holePool->objectAtIndex(hole))->getPosition().y + 37));
							oldMan->appear(hole);
							_countOldManAppear++;
						}
					}
				}
			}
		}
		// Check Collision
		checkCollision();
	}
}

void ScreenGameplay::loadLevel(int level)
{
	_level = level;

	CCDictionary* levelData = (CCDictionary*)_levelPool->objectAtIndex(_level - 1);

	// Load data
	_numberMouseTarget = levelData->valueForKey("NumberMouseTarget")->intValue();
	_numberComboTarget = levelData->valueForKey("NumberComboTarget")->intValue();
	_numberDiamondTarget = levelData->valueForKey("NumberDiamondTarget")->intValue();

	_numberMouseAppearance = levelData->valueForKey("NumberMouseAppearance")->intValue();
	_numberMouseOnScreen = levelData->valueForKey("NumberMouseOnScreen")->intValue();
	_durationMouseDelay[0] = levelData->valueForKey("DurationMouseDelay1")->floatValue();
	_durationMouseDelay[1] = levelData->valueForKey("DurationMouseDelay2")->floatValue();
	_durationMouseAppearance[0] = levelData->valueForKey("DurationMouseAppearance1")->floatValue();
	_durationMouseAppearance[1] = levelData->valueForKey("DurationMouseAppearance2")->floatValue();

	_numberOldManAppearance = levelData->valueForKey("NumberOldManAppearance")->intValue();
	_numberOldManOnScreen = levelData->valueForKey("NumberOldManOnScreen")->intValue();
	_durationOldManDelay[0] = levelData->valueForKey("DurationOldManDelay1")->floatValue();
	_durationOldManDelay[1] = levelData->valueForKey("DurationOldManDelay2")->floatValue();
	_durationOldManAppearance[0] = levelData->valueForKey("DurationOldManAppearance1")->floatValue();
	_durationOldManAppearance[1] = levelData->valueForKey("DurationOldManAppearance2")->floatValue();

	_numberDiamondOnScreen = levelData->valueForKey("NumberDiamondOnScreen")->intValue();
	_durationDiamondDelay[0] = levelData->valueForKey("DurationDiamondDelay1")->floatValue();
	_durationDiamondDelay[1] = levelData->valueForKey("DurationDiamondDelay2")->floatValue();
	_durationDiamondAppearance[0] = levelData->valueForKey("DurationDiamondAppearance1")->floatValue();
	_durationDiamondAppearance[1] = levelData->valueForKey("DurationDiamondAppearance2")->floatValue();

	_numberBombOnScreen = levelData->valueForKey("NumberBombOnScreen")->intValue();
	_durationBombDelay[0] = levelData->valueForKey("DurationBombDelay1")->floatValue();
	_durationBombDelay[1] = levelData->valueForKey("DurationBombDelay2")->floatValue();
	_durationBombAppearance[0] = levelData->valueForKey("DurationBombAppearance1")->floatValue();
	_durationBombAppearance[1] = levelData->valueForKey("DurationBombAppearance2")->floatValue();

	resetLevel();
}

void ScreenGameplay::resetLevel()
{
	srand(time(0));
	clearLayer(true);

	// Reset state game
	_state = STATEGAMEPLAY::sStart;

	// Reset data
	_numberMouseTargetInGame = _numberMouseTarget;
	_numberComboTargetInGame = _numberComboTarget;
	_numberDiamondTargetInGame = _numberDiamondTarget;
	_countOldManHit = 0;
	_countOldManAppear = 0;
	_countMouseHit = 0;
	_countMouseMiss = 0;
	_countDiamondHit = 0;
	_countCombo = -1;
	_countCurrentCombo = 0;

	_noHitBomb = true;

	// Reset on screen	
	_boardTarget->setVisible(true);	
	_btnBack->setVisible(true);
	_btnStart->setVisible(true);
	CCLabelBMFont* label;
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget);
	label->setPosition(ccp(480, 270));
	label->setString(std::to_string(_numberMouseTarget).c_str());
	label->setVisible(true);	

	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget);
	label->setColor(ccc3(255, 255, 255));
	label->setPosition(ccp(480, 220));
	label->setString(std::to_string(_numberComboTarget).c_str());
	label->setVisible(true);	

	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget);
	label->setColor(ccc3(255, 255, 255));
	label->setPosition(ccp(480, 170));
	label->setString(std::to_string(_numberDiamondTarget).c_str());
	label->setVisible(true);	

	((CCSprite*)this->getChildByTag(kSpriteBackground))->setOpacity(130);
	for(int i=0; i<_holePool->count(); i++)
	{
		((CCSprite*)_holePool->objectAtIndex(i))->setOpacity(130);
	}
}

void ScreenGameplay::clearLayer(bool resetGame)
{
	int count;
	if (resetGame)
	{
		count = _bombPool->count();
		for (int i = 0; i < count; i++)
		{
			((CCSprite*)_bombPool->objectAtIndex(i))->setVisible(false);
		}
	}	

	count = _diamondPool->count();
	for (int i = 0; i < count; i++)
	{
		((CCSprite*)_diamondPool->objectAtIndex(i))->setVisible(false);
	}

	count = _mousePool->count();
	for (int i = 0; i < count; i++)
	{
		((CCSprite*)_mousePool->objectAtIndex(i))->setVisible(false);
	}

	count = _oldManPool->count();
	for (int i = 0; i < count; i++)
	{
		((CCSprite*)_oldManPool->objectAtIndex(i))->setVisible(false);
	}
}

void ScreenGameplay::pauseGame()
{
	_state = STATEGAMEPLAY::sPause;

	_boardMouse->setVisible(false);
	_boardCombo->setVisible(false);
	_boardDiamond->setVisible(false);
	_btnPause->setVisible(false);

	CCLabelBMFont* label;
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget);
	label->setVisible(false);	
	label = (CCLabelBMFont*)this->getChildByTag(kLabelComboAppearOnScreen);
	label->setVisible(false);
	/*
	if (label->isVisible())
	{
		label->runAction(CCSequence::create(CCScaleTo::create(0.075f, 1.7f),
			CCScaleTo::create(0.15f, 0.2f),
			CCCallFuncN::create(this, callfuncN_selector(ScreenGameplay::hideLabelCombo)),
			NULL));
	}	
	*/

	_boardPause->setVisible(true);

	_btnReplay->setPosition(ccp(_screenSize.width / 2 - 130, 130));
	_btnReplay->setVisible(true);
	_btnResume->setPosition(ccp(_screenSize.width / 2, 130));
	_btnResume->setVisible(true);
	_btnMenu->setPosition(ccp(_screenSize.width / 2 + 130, 130));
	_btnMenu->setVisible(true);
}

void ScreenGameplay::loseGame()
{
	_state = STATEGAMEPLAY::sYouLose;
	_noHitBomb = true;
	clearLayer(true);

	_boardMouse->setVisible(false);
	_boardCombo->setVisible(false);
	_boardDiamond->setVisible(false);
	_btnPause->setVisible(false);

	CCLabelBMFont* label;
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelComboAppearOnScreen);
	if (label->isVisible())
	{
		label->runAction(CCSequence::create(CCScaleTo::create(0.075f, 1.7f),
			CCScaleTo::create(0.15f, 0.2f),
			CCCallFuncN::create(this, callfuncN_selector(ScreenGameplay::hideLabelCombo)),
			NULL));
	}

	_boardResult->setVisible(true);
	_boardResult->getChildByTag(kSpriteLabelYouLose)->setVisible(true);
	_boardResult->getChildByTag(kSpriteItemStar0)->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseHit);
	label->setString(to_string(_countMouseHit).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseMiss);
	label->setString(to_string(_countMouseMiss).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelOldManHit);
	label->setString(to_string(_countOldManHit).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelDiamondHit);
	label->setString(to_string(_countDiamondHit).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelCombo);
	label->setString(to_string(_countCurrentCombo).c_str());
	label->setVisible(true);

	_btnReplay->setPosition(ccp(_screenSize.width / 2 - 80, 80));
	_btnReplay->setVisible(true);
	_btnMenu->setPosition(ccp(_screenSize.width / 2 + 80, 80));
	_btnMenu->setVisible(true);
}

void ScreenGameplay::winGame()
{
	_state = STATEGAMEPLAY::sWinGame;
	clearLayer(true);

	_boardMouse->setVisible(false);
	_boardCombo->setVisible(false);
	_boardDiamond->setVisible(false);
	_btnPause->setVisible(false);

	CCLabelBMFont* label;
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget);
	label->setVisible(false);
	label = (CCLabelBMFont*)this->getChildByTag(kLabelComboAppearOnScreen);
	if (label->isVisible())
	{
		label->runAction(CCSequence::create(CCScaleTo::create(0.075f, 1.7f),
			CCScaleTo::create(0.15f, 0.2f),
			CCCallFuncN::create(this, callfuncN_selector(ScreenGameplay::hideLabelCombo)),
			NULL));
	}

	_boardResult->setVisible(true);
	_boardResult->getChildByTag(kSpriteLabelWin)->setVisible(true);
	if (_countMouseMiss == 0 && _countOldManHit == 0)
	{
		_boardResult->getChildByTag(kSpriteItemStar3)->setVisible(true);
		CCUserDefault::sharedUserDefault()->setIntegerForKey(("rateStar" + to_string(_level)).c_str(), 3);
		int tempLevel = _level + 1;
		if (tempLevel > _levelCompleted)
		{
			_levelCompleted = tempLevel;
			CCUserDefault::sharedUserDefault()->setIntegerForKey("levelCompleted", _levelCompleted);
		}
	}
	else if (_countMouseMiss <= 3 && _countOldManHit <= 1)
	{
		_boardResult->getChildByTag(kSpriteItemStar2)->setVisible(true);
		CCUserDefault::sharedUserDefault()->setIntegerForKey(("rateStar" + to_string(_level)).c_str(), 2);
		int tempLevel = _level + 1;
		if (tempLevel > _levelCompleted)
		{
			_levelCompleted = tempLevel;
			CCUserDefault::sharedUserDefault()->setIntegerForKey("levelCompleted", _levelCompleted);
		}
	}
	else
	{
		_boardResult->getChildByTag(kSpriteItemStar1)->setVisible(true);
		CCUserDefault::sharedUserDefault()->setIntegerForKey(("rateStar" + to_string(_level)).c_str(), 1);
		int tempLevel = _level + 1;
		if (tempLevel > _levelCompleted)
		{
			_levelCompleted = tempLevel;
			CCUserDefault::sharedUserDefault()->setIntegerForKey("levelCompleted", _levelCompleted);
		}
	}

	label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseHit);
	label->setString(to_string(_countMouseHit).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseMiss);
	label->setString(to_string(_countMouseMiss).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelOldManHit);
	label->setString(to_string(_countOldManHit).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelDiamondHit);
	label->setString(to_string(_countDiamondHit).c_str());
	label->setVisible(true);

	label = (CCLabelBMFont*)this->getChildByTag(kLabelCombo);
	label->setString(to_string(_countCurrentCombo).c_str());
	label->setVisible(true);

	_btnReplay->setPosition(ccp(_screenSize.width / 2 - 120, 80));
	_btnReplay->setVisible(true);
	_btnMenu->setPosition(ccp(_screenSize.width / 2, 80));
	_btnMenu->setVisible(true);
	_btnNext->setPosition(ccp(_screenSize.width/2 + 120, 80));
	_btnNext->setVisible(true);
}

void ScreenGameplay::ccTouchesBegan(CCSet* pTouches, CCEvent* event)
{
	CCTouch* touch = (CCTouch*)pTouches->anyObject();
	CCPoint tap = touch->getLocation();
	if(touch)
	{		
		CCRect boundary;
		// Handle touches
		CCSprite* button;
		CCSprite* buttonPress;

		for(int i=0; i<_buttons->count(); i++)
		{
			button = (CCSprite*)_buttons->objectAtIndex(i);
			if(!button->isVisible())
				continue;

			boundary = button->boundingBox();
			if(boundary.containsPoint(tap))
			{
				buttonPress = (CCSprite*)button->getChildByTag(kSpriteBtnStatePress);
				buttonPress->setVisible(true);
			}
		}
	}

	if(_state == STATEGAMEPLAY::sInGame)
	{		
		if(!_btnPause->boundingBox().containsPoint(tap))
			_hammer->appear(tap);
		//if(!_btnPause->boundingBox().containsPoint(tap))			
	}
}

void ScreenGameplay::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	CCTouch* touch = (CCTouch*)pTouches->anyObject();
	if (touch)
	{
		CCPoint tap = touch->getLocation();
		CCSprite* sprite;
		for (int i = 0; i < _buttons->count(); i++)
		{
			sprite = (CCSprite*)_buttons->objectAtIndex(i);
			// Move out
			if (sprite->getChildByTag(kSpriteBtnStatePress)->isVisible())
			{
				if (!sprite->boundingBox().containsPoint(tap))
				{
					sprite->getChildByTag(kSpriteBtnStatePress)->setVisible(false);
				}
			}
		}
	}
}

void ScreenGameplay::ccTouchesEnded(CCSet* pTouches, CCEvent* event)
{
	CCTouch* touch = (CCTouch*)pTouches->anyObject();
	if(touch)
	{
		CCPoint tap = touch->getLocation();
		CCRect boundary;
		// Handle touches
		CCSprite* button;
		CCSprite* buttonPress;

		for(int i=0; i<_buttons->count(); i++)
		{
			button = (CCSprite*)_buttons->objectAtIndex(i);
			if(!button->isVisible())
				continue;

			boundary = button->boundingBox();
			if(boundary.containsPoint(tap))
			{
				buttonPress = (CCSprite*)button->getChildByTag(kSpriteBtnStatePress);
				buttonPress->setVisible(false);

				CCLabelBMFont* label;
				CCScene* newScene;
				switch (button->getTag())
				{
				case kSpriteBtnStart:
					_state = STATEGAMEPLAY::sInGame;
					
					// Set elements invisible on state start
					_boardTarget->setVisible(false);
					_btnBack->setVisible(false);
					_btnStart->setVisible(false);
					((CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget))->setVisible(false);
					((CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget))->setVisible(false);
					((CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget))->setVisible(false);

					// Set elements visible on state ingame
					_boardMouse->setVisible(true);
					_boardCombo->setVisible(true);
					_boardDiamond->setVisible(true);					
					_btnPause->setVisible(true);
					
					label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget);
					label->setPosition(ccp(72, _screenSize.height - 20));
					label->setString(to_string(_numberMouseTargetInGame).c_str());
					label->setVisible(true);

					label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget);
					label->setPosition(ccp(72, _screenSize.height - 75));
					label->setColor(ccc3(255, 255, 255));
					label->setString(to_string(_numberComboTargetInGame).c_str());
					label->setVisible(true);

					label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget);
					label->setPosition(ccp(72, _screenSize.height - 120));
					if (_numberDiamondTargetInGame == 0)
					{
						label->setColor(ccc3(255, 255, 255));
						label->setString("V");
					}
					else
					{
						label->setColor(ccc3(255, 255, 255));
						label->setString(to_string(_numberDiamondTargetInGame).c_str());
					}					
					label->setVisible(true);

					((CCSprite*)this->getChildByTag(kSpriteBackground))->setOpacity(255);
					for(int i=0; i<_holePool->count(); i++)
					{
						((CCSprite*)_holePool->objectAtIndex(i))->setOpacity(255);
					}
					break;
				case kSpriteBtnBack:
					newScene = CCTransitionMoveInL::create(0.2f, ScreenLevel::scene());
					CCDirector::sharedDirector()->replaceScene(newScene);
					break;
				case kSpriteBtnPause:
					CCDirector::sharedDirector()->pause();
					pauseGame();
					break;
				case kSpriteBtnReplay:
					if (_state == STATEGAMEPLAY::sPause)
					{
						_boardPause->setVisible(false);
						_btnReplay->setVisible(false);
						_btnResume->setVisible(false);
						_btnMenu->setVisible(false);
						CCDirector::sharedDirector()->resume();
					}
					else if (_state == STATEGAMEPLAY::sYouLose)
					{
						_boardResult->setVisible(false);
						_boardResult->getChildByTag(kSpriteLabelYouLose)->setVisible(false);
						_boardResult->getChildByTag(kSpriteItemStar0)->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseMiss);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelOldManHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelDiamondHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelCombo);
						label->setVisible(false);

						_btnReplay->setVisible(false);
						_btnMenu->setVisible(false);
					}
					else if (_state == STATEGAMEPLAY::sWinGame)
					{
						_boardResult->setVisible(false);
						_boardResult->getChildByTag(kSpriteLabelWin)->setVisible(false);
						if (_countMouseMiss == 0 && _countOldManHit == 0)
						{
							_boardResult->getChildByTag(kSpriteItemStar3)->setVisible(false);
						}
						else if (_countMouseMiss <= 3 && _countOldManHit <= 1)
						{
							_boardResult->getChildByTag(kSpriteItemStar2)->setVisible(false);
						}
						else
						{
							_boardResult->getChildByTag(kSpriteItemStar1)->setVisible(false);
						}

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseMiss);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelOldManHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelDiamondHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelCombo);
						label->setVisible(false);

						_btnReplay->setVisible(false);
						_btnMenu->setVisible(false);
						_btnNext->setVisible(false);
					}					
					resetLevel();
					break;
				case kSpriteBtnResume:
					CCDirector::sharedDirector()->resume();
					_state = STATEGAMEPLAY::sInGame;

					_boardPause->setVisible(false);
					_btnReplay->setVisible(false);
					_btnResume->setVisible(false);
					_btnMenu->setVisible(false);

					_boardMouse->setVisible(true);
					_boardCombo->setVisible(true);
					_boardDiamond->setVisible(true);
					_btnPause->setVisible(true);

					CCLabelBMFont* label;
					label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget);
					label->setVisible(true);
					label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget);
					label->setVisible(true);
					label = (CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget);
					label->setVisible(true);								
					break;
				case kSpriteBtnMenu:					
					if (_state == STATEGAMEPLAY::sPause)
					{
						_boardPause->setVisible(false);
						_btnReplay->setVisible(false);
						_btnResume->setVisible(false);
						_btnMenu->setVisible(false);
						CCDirector::sharedDirector()->resume();
					} else if (_state == STATEGAMEPLAY::sYouLose)
					{
						_boardResult->setVisible(false);
						_boardResult->getChildByTag(kSpriteLabelYouLose)->setVisible(false);
						_boardResult->getChildByTag(kSpriteItemStar0)->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseMiss);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelOldManHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelDiamondHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelCombo);
						label->setVisible(false);

						_btnReplay->setVisible(false);
						_btnMenu->setVisible(false);
					} else if (_state == STATEGAMEPLAY::sWinGame)
					{
						_boardResult->setVisible(false);
						_boardResult->getChildByTag(kSpriteLabelWin)->setVisible(false);
						if (_countMouseMiss == 0 && _countOldManHit == 0)
						{
							_boardResult->getChildByTag(kSpriteItemStar3)->setVisible(false);
						}
						else if (_countMouseMiss <= 3 && _countOldManHit <= 1)
						{
							_boardResult->getChildByTag(kSpriteItemStar2)->setVisible(false);
						}
						else
						{
							_boardResult->getChildByTag(kSpriteItemStar1)->setVisible(false);
						}

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseMiss);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelOldManHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelDiamondHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelCombo);
						label->setVisible(false);

						_btnReplay->setVisible(false);
						_btnMenu->setVisible(false);
						_btnNext->setVisible(false);
					}	
					
					newScene = CCTransitionMoveInL::create(0.2f, ScreenLevel::scene());
					CCDirector::sharedDirector()->replaceScene(newScene);
					break;
				case kSpriteBtnNext:
						_boardResult->setVisible(false);
						_boardResult->getChildByTag(kSpriteLabelWin)->setVisible(false);
						if (_countMouseMiss == 0 && _countOldManHit == 0)
						{
							_boardResult->getChildByTag(kSpriteItemStar3)->setVisible(false);
						}
						else if (_countMouseMiss <= 3 && _countOldManHit <= 1)
						{
							_boardResult->getChildByTag(kSpriteItemStar2)->setVisible(false);
						}
						else
						{
							_boardResult->getChildByTag(kSpriteItemStar1)->setVisible(false);
						}

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelMouseMiss);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelOldManHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelDiamondHit);
						label->setVisible(false);

						label = (CCLabelBMFont*)this->getChildByTag(kLabelCombo);
						label->setVisible(false);

						_btnReplay->setVisible(false);
						_btnMenu->setVisible(false);
						_btnNext->setVisible(false);

						loadLevel(_level + 1);
					break;
				default:
					break;
				}
			}
		}
	}
}

void ScreenGameplay::setHoleBlank(int hole)
{
	((Hole*)_holePool->objectAtIndex(hole))->setBlank();
}

void ScreenGameplay::increaseMouseMiss()
{
	_countCombo = -1;

	CCLabelBMFont* label = (CCLabelBMFont*)this->getChildByTag(kLabelComboAppearOnScreen);
	label->runAction(CCSequence::create(CCScaleTo::create(0.075f, 1.7f), 
										CCScaleTo::create(0.15f, 0.2f), 
										CCCallFuncN::create(this, callfuncN_selector(ScreenGameplay::hideLabelCombo)), 
										NULL));

	_countMouseMiss++;
	if (_countMouseHit + _countMouseMiss >= _numberMouseAppearance)
	{
		loseGame();
	}
}

void ScreenGameplay::increaseMouseHit()
{
	_countMouseHit++;
	if (_countMouseHit + _countMouseMiss >= _numberMouseAppearance)
	{
		loseGame();
	}
	_numberMouseTargetInGame--;
	((CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget))->setString(to_string(_numberMouseTargetInGame).c_str());
	if (_numberMouseTargetInGame <= 0)
	{
		// WinGame
		winGame();
	}
}

void ScreenGameplay::hideLabelCombo(CCNode* pSender)
{
	pSender->setVisible(false);
}

void ScreenGameplay::createScreen()
{
	CCSprite* bgSprite = CCSprite::create("bg_gameplay.png");
	bgSprite->setPosition(ccp(_screenSize.width * 0.5f, _screenSize.height * 0.5f));
	this->addChild(bgSprite, kBackground, kSpriteBackground);
	
	_gameBatchNode = CCSpriteBatchNode::create("sheet_sprite.png", 20);
	this->addChild(_gameBatchNode, kMiddleground);

	//-------------------------- BOARD ----------------------------------
 	_boardTarget = CCSprite::createWithSpriteFrameName("board_target.png");
	_boardTarget->setPosition(ccp(_screenSize.width * 0.5f, _screenSize.height * 0.5f));
	_boardTarget->setVisible(false);
	_gameBatchNode->addChild(_boardTarget, kForeground);

	_boardPause = CCSprite::createWithSpriteFrameName("board_pause.png");
	_boardPause->setPosition(ccp(_screenSize.width * 0.5f, _screenSize.height * 0.5f));
	_boardPause->setVisible(false);
	_gameBatchNode->addChild(_boardPause, kForeground);

	_boardResult = CCSprite::createWithSpriteFrameName("board_result.png");
	_boardResult->setPosition(ccp(_screenSize.width * 0.5f, _screenSize.height * 0.5f));
	_boardResult->setVisible(false);
	_gameBatchNode->addChild(_boardResult, kForeground);
	
	_boardMouse = CCSprite::createWithSpriteFrameName("board_mouse.png");
	_boardMouse->setAnchorPoint(ccp(0, 1));
	_boardMouse->setPosition(ccp(10, _screenSize.height -10));
	_boardMouse->setVisible(false);
	_gameBatchNode->addChild(_boardMouse, kForeground);

	_boardCombo = CCSprite::createWithSpriteFrameName("board_combo.png");
	_boardCombo->setAnchorPoint(ccp(0, 1));
	_boardCombo->setPosition(ccp(10, _screenSize.height - 15 - _boardMouse->boundingBox().size.height));
	_boardCombo->setVisible(false);
	_gameBatchNode->addChild(_boardCombo, kForeground);

	_boardDiamond = CCSprite::createWithSpriteFrameName("board_diamond.png");
	_boardDiamond->setAnchorPoint(ccp(0, 1));
	_boardDiamond->setPosition(ccp(10, _screenSize.height - 20 - _boardMouse->boundingBox().size.height - _boardCombo->boundingBox().size.height));
	_boardDiamond->setVisible(false);
	_gameBatchNode->addChild(_boardDiamond, kForeground);

	//-------------------------- ITEM ON BOARD----------------------------------
	CCSprite*  item;	
	item = CCSprite::createWithSpriteFrameName("label_win.png");
	item->setAnchorPoint(ccp(0.5f, 0));
	item->setPosition(ccp(_boardResult->boundingBox().size.width / 2, 184));
	item->setVisible(false);
	_boardResult->addChild(item, kMiddleground, kSpriteLabelWin);

	item = CCSprite::createWithSpriteFrameName("label_youlose.png");
	item->setAnchorPoint(ccp(0.5f, 0));
	item->setPosition(ccp(_boardResult->boundingBox().size.width / 2, 164));
	item->setVisible(false);
	_boardResult->addChild(item, kMiddleground, kSpriteLabelYouLose);

	item = CCSprite::createWithSpriteFrameName("item_star0.png");
	item->setPosition(ccp(_boardResult->boundingBox().size.width / 2, 158));
	item->setVisible(false);
	_boardResult->addChild(item, kMiddleground, kSpriteItemStar0);

	item = CCSprite::createWithSpriteFrameName("item_star1.png");
	item->setPosition(ccp(_boardResult->boundingBox().size.width / 2, 158));
	item->setVisible(false);
	_boardResult->addChild(item, kMiddleground, kSpriteItemStar1);

	item = CCSprite::createWithSpriteFrameName("item_star2.png");
	item->setPosition(ccp(_boardResult->boundingBox().size.width / 2, 158));
	item->setVisible(false);
	_boardResult->addChild(item, kMiddleground, kSpriteItemStar2);

	item = CCSprite::createWithSpriteFrameName("item_star3.png");
	item->setPosition(ccp(_boardResult->boundingBox().size.width / 2, 158));
	item->setVisible(false);
	_boardResult->addChild(item, kMiddleground, kSpriteItemStar3);

	item = CCSprite::createWithSpriteFrameName("item_result.png");
	item->setPosition(ccp(_boardResult->boundingBox().size.width / 2, 84));
	_boardResult->addChild(item, kMiddleground, kSpriteItemResult);
	

	//-------------------------- BUTTONS ----------------------------------
	_buttons = CCArray::createWithCapacity(7);
	_buttons->retain();

	CCSprite* button;
	_btnStart = CCSprite::createWithSpriteFrameName("btn_start_normal.png");
	_btnStart->setPosition(ccp(550, 100));
	_btnStart->setVisible(false);
	_buttons->addObject(_btnStart);
	_gameBatchNode->addChild(_btnStart, kForeground, kSpriteBtnStart);
	button = CCSprite::createWithSpriteFrameName("btn_start_press.png");
	button->setAnchorPoint(ccp(0, 0));
	button->setVisible(false);
	_btnStart->addChild(button, kForeground, kSpriteBtnStatePress);

	_btnBack = CCSprite::createWithSpriteFrameName("btn_back_normal.png");
	_btnBack->setAnchorPoint(ccp(0, 0));
	_btnBack->setPosition(ccp(20, 20));
	_btnBack->setVisible(false);
	_buttons->addObject(_btnBack);
	_gameBatchNode->addChild(_btnBack, kForeground, kSpriteBtnBack);
	button = CCSprite::createWithSpriteFrameName("btn_back_press.png");
	button->setAnchorPoint(ccp(0, 0));	
	button->setVisible(false);
	_btnBack->addChild(button, kForeground, kSpriteBtnStatePress);

	_btnPause = CCSprite::createWithSpriteFrameName("btn_pause_normal.png");
	_btnPause->setAnchorPoint(ccp(1, 1));
	_btnPause->setPosition(ccp(_screenSize.width, _screenSize.height));
	_btnPause->setVisible(false);
	_buttons->addObject(_btnPause);
	_gameBatchNode->addChild(_btnPause, kForeground, kSpriteBtnPause);
	button = CCSprite::createWithSpriteFrameName("btn_pause_press.png");
	button->setAnchorPoint(ccp(0, 0));
	button->setVisible(false);
	_btnPause->addChild(button, kForeground, kSpriteBtnStatePress);

	_btnMenu = CCSprite::createWithSpriteFrameName("btn_menu_normal.png");
	_btnMenu->setPosition(ccp(0, 0));
	_btnMenu->setVisible(false);
	_buttons->addObject(_btnMenu);
	_gameBatchNode->addChild(_btnMenu, kForeground, kSpriteBtnMenu);
	button = CCSprite::createWithSpriteFrameName("btn_menu_press.png");
	button->setAnchorPoint(ccp(0, 0));
	button->setVisible(false);
	_btnMenu->addChild(button, kForeground, kSpriteBtnStatePress);

	_btnNext = CCSprite::createWithSpriteFrameName("btn_next_normal.png");
	_btnNext->setRotation(180);
	_btnNext->setPosition(ccp(0, 0));
	_btnNext->setVisible(false);
	_buttons->addObject(_btnNext);
	_gameBatchNode->addChild(_btnNext, kForeground, kSpriteBtnNext);
	button = CCSprite::createWithSpriteFrameName("btn_next_press.png");
	button->setAnchorPoint(ccp(0, 0));
	button->setVisible(false);
	_btnNext->addChild(button, kForeground, kSpriteBtnStatePress);

	_btnReplay = CCSprite::createWithSpriteFrameName("btn_replay_normal.png");
	_btnReplay->setPosition(ccp(0, 0));
	_btnReplay->setVisible(false);
	_buttons->addObject(_btnReplay);
	_gameBatchNode->addChild(_btnReplay, kForeground, kSpriteBtnReplay);
	button = CCSprite::createWithSpriteFrameName("btn_replay_press.png");
	button->setAnchorPoint(ccp(0, 0));
	button->setVisible(false);
	_btnReplay->addChild(button, kForeground, kSpriteBtnStatePress);

	_btnResume = CCSprite::createWithSpriteFrameName("btn_resume_normal.png");
	_btnResume->setPosition(ccp(0, 0));
	_btnResume->setVisible(false);
	_buttons->addObject(_btnResume);
	_gameBatchNode->addChild(_btnResume, kForeground, kSpriteBtnResume);
	button = CCSprite::createWithSpriteFrameName("btn_resume_press.png");
	button->setAnchorPoint(ccp(0, 0));
	button->setVisible(false);
	_btnResume->addChild(button, kForeground, kSpriteBtnStatePress);

	//-------------------------- LABELS ----------------------------------
	CCLabelBMFont* label;
	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setAnchorPoint(ccp(0, 1));	
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelNumberMouseTarget);

	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setAnchorPoint(ccp(0, 1));	
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelNumberComboTarget);

	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setAnchorPoint(ccp(0, 1));	
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelNumberDiamondTarget);

	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setPosition(ccp(248, 165));
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelMouseHit);

	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setPosition(ccp(326, 165));
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelOldManHit);

	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setPosition(ccp(404, 165));
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelDiamondHit);

	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setPosition(ccp(482, 165));
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelCombo);

	label = CCLabelBMFont::create(std::string("0").c_str(), "showg_score.fnt");
	label->setPosition(ccp(570, 165));
	label->setVisible(false);	
	this->addChild(label, kForeground, kLabelMouseMiss);

	label = CCLabelBMFont::create(std::string(" ").c_str(), "showg_score.fnt");
	label->setPosition(ccp(_screenSize.width * 0.5f, _screenSize.height * 0.92f));
	label->setVisible(false);
	this->addChild(label, kForeground, kLabelComboAppearOnScreen);

}

void ScreenGameplay::createPools()
{	
	// ------------------- HOLE -----------------------------	
	int i;
	_holePool = CCArray::createWithCapacity(14);
	_holePool->retain();
	Hole* hole;
	float posX = 0, posY = 0;	
	for (i = 0; i < 14; i++)
	{		
		if (i < 4)
		{			
			switch (i)
			{
			case 0:
				posX = 105;
				break;
			case 1:
				posX = 296;
				break;
			case 2:
				posX = 495;
				break;
			case 3:
				posX = 682;
				break;
			default:
				posX = 0;				
				break;
			}
			posY = 80;
		}
		else if (i < 7)
		{
			switch (i)
			{
			case 4:
				posX = 210;
				break;
			case 5:
				posX = 404;
				break;
			case 6:
				posX = 600;
				break;			
			default:
				posX = 0;
				break;
			}
			posY = 170;
		}
		else if (i < 11)
		{
			switch (i)
			{
			case 7:
				posX = 105;
				break;
			case 8:
				posX = 296;
				break;
			case 9:
				posX = 495;
				break;
			case 10:
				posX = 682;
				break;
			default:
				posX = 0;
				break;
			}
			posY = 242;
		}
		else
		{
			switch (i)
			{
			case 11:
				posX = 210;
				break;
			case 12:
				posX = 404;
				break;
			case 13:
				posX = 600;
				break;
			default:
				posX = 0;
				break;
			}
			posY = 314;
		}
		hole = Hole::create();
		hole->setPosition(ccp(posX, posY));
		_holePool->addObject(hole);
		_gameBatchNode->addChild(hole, kBackground, kSpriteObjHole);
	}

	// ------------------- BOMB -----------------------------
	_bombPool = CCArray::createWithCapacity(1);
	_bombPool->retain();
	Bomb* bomb;
	for (int i = 0; i < 1; i++)
	{
		bomb = Bomb::create(this);
		_bombPool->addObject(bomb);
		_gameBatchNode->addChild(bomb, kMiddleground, kSpriteObjBomb);
		CCSprite* sprite;
		CCSize size = bomb->boundingBox().size;
		for (int j = 0; j < 12; j++)
		{
			sprite = CCSprite::createWithSpriteFrameName("obj_ray.png");
			sprite->setAnchorPoint(ccp(0, 0.5f));
			sprite->setPosition(ccp(size.width / 2, size.height / 2));
			sprite->setRotation(j*30);
			sprite->setVisible(false);
			bomb->addChild(sprite, kMiddleground, kSpriteObjRay);
		}
		sprite = CCSprite::createWithSpriteFrameName("obj_bom_05.png");
		sprite->setPosition(ccp(size.width / 2, size.height / 2));
		sprite->setVisible(false);
		bomb->addChild(sprite, kForeground, kSpriteObjBombExplosion);
	}
	// ------------------- DIAMOND --------------------------
	_diamondPool = CCArray::createWithCapacity(1);
	_diamondPool->retain();
	Diamond* diamond;
	for (int i = 0; i < 1; i++)
	{
		diamond = Diamond::create(this);
		_diamondPool->addObject(diamond);
		_gameBatchNode->addChild(diamond, kMiddleground, kSpriteObjDiamond);
	}
	// ------------------- MOUSE ----------------------------
	_mousePool = CCArray::createWithCapacity(3);
	_mousePool->retain();
	Mouse* mouse;
	for (i = 0; i < 3; i++)
	{
		mouse = Mouse::create(this);
		_mousePool->addObject(mouse);
		_gameBatchNode->addChild(mouse, kMiddleground, kSpriteObjMouse);
	}
	// ------------------- OLDMAN ---------------------------
	_oldManPool = CCArray::createWithCapacity(1);
	_oldManPool->retain();
	OldMan* oldMan;
	for (int i = 0; i < 1; i++)
	{
		oldMan = OldMan::create(this);
		_oldManPool->addObject(oldMan);
		_gameBatchNode->addChild(oldMan, kMiddleground, kSpriteObjOldMan);
	}
	// ------------------- HAMMER ---------------------------
	_hammer = Hammer::create();
	_gameBatchNode->addChild(_hammer, kMiddleground, kSpriteObjHammer);
}

void ScreenGameplay::createActions()
{
	Bomb::createAction();
	Hammer::createAction();
	Diamond::createAction();
	Mouse::createAction();
	OldMan::createAction();
}

void ScreenGameplay::checkCollision()
{
	if (_hammer->isVisible())
	{
		// Check collision with Bomb
		Bomb* bomb;
		for (int i = 0; i < _bombPool->count(); i++)
		{
			bomb = (Bomb*)_bombPool->objectAtIndex(i);
			if (bomb->isVisible() && bomb->getCanHit())
			{				
				Circle circle(ccp(_hammer->getPositionX(), _hammer->getPositionY()), RADIUS_HAMMER);
				if (circle.checkCollisionWithRect(bomb->boundingBox()))
				{					
					clearLayer(false);
					bomb->effectWasHit();
					_noHitBomb = false;
				}
					
			}
		}

		// Check collision with Diamond
		Diamond* diamond;
		for (int i = 0; i < _diamondPool->count(); i++)
		{
			diamond = (Diamond*)_diamondPool->objectAtIndex(i);
			if (diamond->isVisible() && diamond->getCanHit())
			{
				Circle circle(ccp(_hammer->getPositionX(), _hammer->getPositionY()), RADIUS_HAMMER);
				if (circle.checkCollisionWithRect(diamond->boundingBox()))
				{
					_countDiamondHit++;
					_numberDiamondTargetInGame--;
					if (_numberDiamondTargetInGame <= 0)
					{ 
						((CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget))->setColor(ccc3(255, 215, 0));
						((CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget))->setString("V");
					}
					else
					{
						((CCLabelBMFont*)this->getChildByTag(kLabelNumberDiamondTarget))->setString(to_string(_numberDiamondTargetInGame).c_str());
					}
					diamond->effectWasHit(_boardDiamond->getPosition());
				}					
			}
		}

		// Check collision with Mouse
		Mouse* mouse;
		for (int i = 0; i < _mousePool->count(); i++)
		{
			mouse = (Mouse*)_mousePool->objectAtIndex(i);
			if (mouse->isVisible() && mouse->getCanHit())
			{
				Circle circle(ccp(_hammer->getPositionX(), _hammer->getPositionY()), RADIUS_HAMMER);
				if (circle.checkCollisionWithRect(mouse->boundingBox()))
				{
					_countCombo++;			
					if (_countCombo > 0)
					{
						// Show label combo on the screen
						CCLabelBMFont* label = (CCLabelBMFont*)this->getChildByTag(kLabelComboAppearOnScreen);
						label->setVisible(true);
						label->setString(to_string(_countCombo).c_str());
						label->setScale(0.2f);
						label->runAction(CCSequence::create(CCScaleTo::create(0.15f, 1.7f), 
															CCScaleTo::create(0.075f, 1.0f),
															NULL));
					}

					if (_countCombo > _countCurrentCombo)
					{
						_countCurrentCombo = _countCombo;
						if (_countCurrentCombo >= _numberComboTargetInGame)
						{
							((CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget))->setColor(ccc3(255, 215, 0));
							((CCLabelBMFont*)this->getChildByTag(kLabelNumberComboTarget))->setString("V");
						}
					}
					mouse->dizzy();
				}					
			}
		}

		// Check collision with OldMan
		OldMan* oldman;
		for (int i = 0; i < _oldManPool->count(); i++)
		{
			oldman = (OldMan*)_oldManPool->objectAtIndex(i);
			if (oldman->isVisible() && oldman->getCanHit())
			{
				Circle circle(ccp(_hammer->getPositionX(), _hammer->getPositionY()), RADIUS_HAMMER);
				if (circle.checkCollisionWithRect(oldman->boundingBox()))
				{
					_numberMouseTargetInGame += NUMBER_INCREASE_MOUSE;
					((CCLabelBMFont*)this->getChildByTag(kLabelNumberMouseTarget))->setString(to_string(_numberMouseTargetInGame).c_str());
					_countOldManHit++;
					oldman->dizzy();					
				}					
			}
		}
	}
}
